package com.gxa.auth.service.impl;

import com.gxa.auth.service.AuthenticationService;
import com.gxa.auth.util.JwtUtil;
import dto.ResultDTO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import param.AuthParam;
import util.Response;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    @Resource(name = "objectRedisTemplate")
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResultDTO authentication(AuthParam authParam) {
        Integer userId = JwtUtil.getUserId(authParam.getToken());
        if(!redisTemplate.hasKey("token:"+userId)){
            return Response.error(401,"token已过期");
        }
        List<String> o = (List<String>) redisTemplate.opsForHash().get("token:" + userId, authParam.getToken());
        if(o.toString().contains(authParam.getUri())){
            return Response.success(200,"拥有权限");
        }
        return Response.error(201,"没有权限");
    }
}
